package com.itheima.job;

import com.itheima.delaytask.constants.ZKdirConstants;
import com.itheima.delaytask.feign.ZhzTaskService;
import com.itheima.utils.ZKSelectMasterUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @program: virtual-trade-parent
 * @description: 通过job独立模块统一调用定时任务
 * @author: zhanghz001
 * @create: 2021-07-11 22:49
 **/
@Slf4j
// @Component
public class ZhzDelayTaskJob implements InitializingBean {
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private ZhzTaskService taskService;
    @Autowired
    private ZKSelectMasterUtil zkSelectMasterUtil;
    
    @Scheduled(cron = "*/1 * * * * ?")
    public void refresh() {
        if (zkSelectMasterUtil.checkMaster(ZKdirConstants.job_leaderPath)) {
            
            threadPoolTaskExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    log.info("job节点:{}, 开始进行定时刷新", LocalDateTime.now().format(
                            DateTimeFormatter.ISO_LOCAL_DATE_TIME
                    ));
                    taskService.refresh();
                }
            });
        } else {
            log.info("job 从节点备份");
        }
    }
    
    @Override
    public void afterPropertiesSet() throws Exception {
        zkSelectMasterUtil.selectMaster(ZKdirConstants.job_leaderPath);
        
    }
}
